package tpo.vista.empleados;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import tpo.controlador.Sistema;
import tpo.to.AutomovilDTO;
import tpo.to.ClienteDTO;

public class AltaAlquilerSinReserva extends JFrame {
	
	private Sistema sistema;
	private SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
	
	public AltaAlquilerSinReserva(Sistema sistema) {
		super("Alta Alquiler");
		this.sistema = sistema;
		init();
		setVisible(true);
	}

	private void init() {
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setLayout(new GridBagLayout());
		GridBagConstraints gc = new GridBagConstraints();
		
		List<ClienteDTO> clientesDTO = sistema.getClientesDTO();		
		List<AutomovilDTO> automovilesDTO = sistema.getAutomovilDTO();
		
		JLabel labelCliente = new JLabel("Cliente");
		JLabel labelInicio = new JLabel("Fecha Inicio (dd/mm/yyyy)");
		JLabel labelFin = new JLabel("Fecha Fin (dd/mm/yyyy)");
		JLabel labelAutomovil = new JLabel("Automovil");
		JLabel labelInspeccion = new JLabel("Inspeccion");
		final JComboBox clientesCombo = new JComboBox(clientesDTO.toArray());
		final JTextField fieldInicio = new JTextField("");
		final JTextField fieldFin = new JTextField("");
		final JComboBox automovilesCombo = new JComboBox(automovilesDTO.toArray());
		final JTextField fieldInspeccion = new JTextField("");
		JButton button = new JButton("Crear");
		
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evento) {
				try {
			        ClienteDTO clienteDTO = (ClienteDTO) clientesCombo.getSelectedItem();
			        AutomovilDTO automovilDTO = (AutomovilDTO) automovilesCombo.getSelectedItem();
			        Date inicio = formatter.parse(fieldInicio.getText());
			        Date fin = formatter.parse(fieldFin.getText());
					sistema.registrarAlquiler(clienteDTO.getCodigo(), inicio,
							fin, automovilDTO.getCodigo(),
							fieldInspeccion.getText());
					dispose();
					System.out.println("Se ha creado la reserva.");
				} catch (Exception e) {
					System.out.println("Error al crear reserva.");
					e.printStackTrace();
				}
			}			
		});
		
		gc.gridx = 0;	// Columna
		gc.gridy = 0;	// Fila
		add(labelCliente, gc);		
		gc.gridx = 1;
		gc.gridy = 0;
		add(clientesCombo, gc);
		
		gc.gridx = 0;
		gc.gridy = 1;
		add(labelInicio, gc);		
		gc.gridx = 1;
		gc.gridy = 1;
		fieldInicio.setColumns(10);
		add(fieldInicio, gc);
		
		gc.gridx = 0;
		gc.gridy = 2;
		add(labelFin, gc);		
		gc.gridx = 1;
		gc.gridy = 2;
		fieldFin.setColumns(10);
		add(fieldFin, gc);
		
		gc.gridx = 0;
		gc.gridy = 3;
		add(labelAutomovil, gc);		
		gc.gridx = 1;
		gc.gridy = 3;
		add(automovilesCombo, gc);
		
		gc.gridx = 0;
		gc.gridy = 4;
		add(labelInspeccion, gc);		
		gc.gridx = 1;
		gc.gridy = 4;
		fieldInspeccion.setColumns(10);
		add(fieldInspeccion, gc);
		
		gc.gridx = 0;
		gc.gridy = 5;
		add(button, gc);

		pack();
		setSize(400, 300);
	}
}
